// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LiveObject.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LiveObjects
{
    #region Usings

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.LiveObjects.Interfaces;

    #endregion

    /// <summary>
    /// The live object.
    /// </summary>
    public abstract class LiveObject : RemotingStub, ILiveObject
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LiveObject"/> class.
        /// </summary>
        /// <param name="descriptor">
        /// The descriptor.
        /// </param>
        /// <param name="container">
        /// The container.
        /// </param>
        public LiveObject(ILiveObjectDescriptor descriptor, ILiveObjectContainer container)
        {
            this.Descriptor = descriptor;
            this.Container = container;

            this.LiveObjectsEventCB =
                new RemotableEventHandler<LiveObjectsEventArgs>("LiveObject-" + descriptor.ID + ".LiveObjectsEventCB");

            if (descriptor is LiveObjectDescriptor.Local)
            {
                var lid = (LiveObjectDescriptor.Local)descriptor;
                var schema = container.GetConfigurationSchema(lid.ClassType, ConfigurationFieldAttribute.DefaultSchema);
                if (schema != null)
                {
                    this.Configuration = new Configuration(descriptor.ConfigurationID, schema, container);
                }

                ////Stdout.WriteLine(
                ////    "** LiveObject Constructor Called from " + AppDomain.CurrentDomain.FriendlyName + " for " +
                ////    descriptor.ID + " " + descriptor.Name + " " + descriptor.Label);
            }

            this.IdentityString = string.Format(
                "ID={0} Name={1} Interface={2} Label={3}", 
                descriptor.ID, 
                descriptor.Name, 
                descriptor.InterfaceType, 
                descriptor.Label);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Configuration.
        /// </summary>
        public IConfiguration Configuration { get; set; }

        /// <summary>
        /// Gets or sets Container.
        /// </summary>
        public ILiveObjectContainer Container { get; set; }

        /// <summary>
        /// Gets or sets Descriptor.
        /// </summary>
        public ILiveObjectDescriptor Descriptor { get; set; }

        /// <summary>
        /// Gets IdentityString.
        /// </summary>
        public string IdentityString { get; private set; }

        /// <summary>
        /// Gets or sets LiveObjectsEventCB.
        /// </summary>
        public RemotableEventHandler<LiveObjectsEventArgs> LiveObjectsEventCB { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        public virtual void Dispose()
        {
        }

        #endregion

        #region Implemented Interfaces

        #region ILiveObject

        /// <summary>
        /// The on activation context.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="activationEventArgs">
        /// The activation Context.
        /// </param>
        public abstract void OnActivation(object sender, ActivationEventArgs activationEventArgs);

        #endregion

        #endregion
    }
}